import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.*;

public class ConfirmDeliveryServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderId = request.getParameter("orderId");
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://127.0.0.1:3306/shop?characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false",
                    "root", "123456");
            String sql = "UPDATE order_details SET is_delivered = 1 WHERE order_id = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, Integer.parseInt(orderId));
            int rowsAffected = stmt.executeUpdate();
            if (rowsAffected > 0) {
                // Redirect to order.jsp with a success message
                request.setAttribute("message", "收货确认成功");
            } else {
                // Redirect to order.jsp with a fail message
                request.setAttribute("message", "收货确认失败");
            }
            // Forward to order.jsp
            RequestDispatcher dispatcher = request.getRequestDispatcher("order.jsp");
            dispatcher.forward(request, response);
        } catch (Exception e) {
            request.setAttribute("message", "服务器错误: " + e.getMessage());
            RequestDispatcher dispatcher = request.getRequestDispatcher("order.jsp");
            dispatcher.forward(request, response);
        } finally {
            try {
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
    }
}